//
//  MainViewController.m
//  weibo_oc
//
//  Created by 岳挺 on 15/6/25.
//  Copyright © 2015年 yueting. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "ProfileTableViewController.h"
#import "DiscoverTableViewController.h"
#import "MessageTableViewController.h"
#import "MainTabBar.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    MainTabBar *tb = [[MainTabBar alloc] init];
    [tb.composeBtn addTarget:self action:@selector(composeBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self setValue:tb forKey:@"tabBar"];
    
    [self addControllers];
}

- (void)composeBtnDidClick
{
    NSLog(@"%s",__func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addControllers
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Controllers.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
    NSLog(@"%@",json);
    
    for (NSDictionary *dict in json) {
        [self addChildController:dict[@"vcName"] title:dict[@"title"] imageName:dict[@"imageName"]];
    }
}

- (void)addChildController:(NSString *)vcName title:(NSString *)title imageName:(NSString *)imageName{
    Class cls = NSClassFromString(vcName);
    UIViewController* controller = [[cls alloc] init];
    controller.title = title;
    controller.tabBarItem.image = [UIImage imageNamed:imageName];
    controller.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_hightlighted",imageName]];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [self addChildViewController:nav];
}

@end
